package com.easylinkin.linkappapi.facerecognition.controller;

import com.easylinkin.linkappapi.facerecognition.dto.OSSPolicy;
import com.easylinkin.linkappapi.facerecognition.service.oss.ObjectStorageOutsideService;
import java.io.IOException;
import java.sql.Date;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

@Slf4j
@RestController
@RequestMapping("/oss")
public class OssController {

  @Autowired
  @Qualifier("oss")
  private ObjectStorageOutsideService ossClient;

  @Value("${oss.dir}")
  private String dir;

  @Value("${oss.bucket}")
  private String bucket;

  @GetMapping("/policy")
  public RestMessage getUploadPolicy() {

    try {
      OSSPolicy respMap = ossClient.generatePostPolicy(dir);
      return RestBuilders.successBuilder().data(respMap).build();

    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }

    return RestBuilders.failureBuilder().message("get upload policy error").build();
  }

  @PostMapping(value = "/uploadMinio")
  public ResponseEntity<?> uploadMinio(@RequestParam("file") MultipartFile file,
      @RequestParam("key") String key) throws IOException {

    ossClient.uploadFile(bucket,file,key);
//    ossClient.uploadImgObject(bucket, fileName, file.getInputStream());
    ResponseEntity<String> ok = ResponseEntity.ok(key);
    return ok;
  }

  @GetMapping(value = "/download/{fileName:.+}")
  public void download(@PathVariable("fileName") String fileName, HttpServletResponse response)
      throws IOException {

    fileName = "/" + fileName;
    int expireTime = 7 * 24 * 60 * 60;
    // 设置这个文件地址的有效时间
    String url = ossClient.generatePresignedUrl(bucket, fileName, expireTime);
    response.sendRedirect(url);
  }

  @GetMapping(value = "/download/{level1}/{level2}/{fileName:.+}")
  public void download(@PathVariable("fileName") String fileName, @PathVariable("level1") String level1,
      @PathVariable("level2") String level2, HttpServletResponse response)
      throws IOException {

    fileName = level1 + "/" + level2 + "/" + fileName;
    int expireTime = 7 * 24 * 60 * 60;
    // 设置这个文件地址的有效时间
    String url = ossClient.generatePresignedUrl(bucket,fileName, expireTime);
    response.sendRedirect(url);
  }

  @GetMapping(value = "/download/{level1}/{level2}/{level3}/{level4}/{fileName:.+}")
  public void download(@PathVariable("fileName") String fileName, @PathVariable("level1") String level1,
      @PathVariable("level2") String level2,@PathVariable("level3") String level3,@PathVariable("level4") String level4, HttpServletResponse response)
      throws IOException {

    fileName = level1 + "/" + level2 + "/"+ level3 + "/"+ level4 + "/" + fileName;
    int expireTime = 7 * 24 * 60 * 60;
    // 设置这个文件地址的有效时间
    String url = ossClient.generatePresignedUrl(bucket,fileName, expireTime);
    response.sendRedirect(url);
  }

}
